//--------------------Sonic CD HUD Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
// Not all of these are used here in the HUD script, some are just used as a way for other objects to globally transmit values


// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Priority
private alias 1 : PRIORITY_ACTIVE

public value UseFlashlight = 0;
public value MouseEnabled = 0;
public value FlashBattery = 5000;

public alias saveRAM[295]: saved.mobile
public alias saveRAM[296]: saved.transmobile

private alias Object.Value0 : Object.Timer 
private alias Object.Value1 : Logo.Xpos
private alias Object.Value2 : Object.MenuChoose
private alias Object.Value3 : Object.ZPos
private alias Object.Value4 : Object.PlayerSelection

private alias object.value6 : object.extraoffset
private alias object.value7 : object.extratarget

private alias 5 : ST.xspeed

private alias 0 : STARTGAME
private alias 1 : ENDLESSMODE
private alias 2 : RETURNTOMENU


private alias 0 : MENU_STARTUP
private alias 1 : MENU_LOWFADEIN
private alias 2 : MENU_LOGOMOVEIN
private alias 3 : MENU_MAINCARD
private alias 4 : MENU_GOTOCHARSEL
private alias 5 : MENU_CHARASEL
private alias 6 : MENU_LOWFADEOUTCHARASEL
private alias 7 : MENU_FADEINTOCHARSEL	//wow what good ordering
private alias 8 : MENU_FADEINTOMAINCARD
private alias 9 : MENU_STARTGAME

reserve function TouchControls

function TouchControls
	object.inkEffect = INK_ALPHA

	temp0 = screen.xsize
	temp0 -= 44
	DrawSpriteScreenXY(49,  26,1 49) //dpad
	temp0 -= 39

	DrawSpriteScreenXY(56, 0, temp0, 8) //pause
	
	
	//movement
	
	temp0 -= 10
	if inputDown.Left==1
		DrawSpriteScreenXY(52,  26, 151)
	endif
	if inputDown.Down==1
		DrawSpriteScreenXY(51,  26, 189)
	end if
	if inputDown.Right==1
		DrawSpriteScreenXY(53,  64, 151)
	endif
	if inputDown.Up==1
		DrawSpriteScreenXY(50,  26, 151)
	endif
	
	temp0 += 15	
	//jump
	if inputDown.buttonA==1
		DrawSpriteScreenXY(55,  temp0, 163)	
	else
		DrawSpriteScreenXY(54,  temp0, 163)
	endif

		temp0 -= 50
		//flashlight
		if inputDown.buttonB==1
			DrawSpriteScreenXY(58,  temp0, 54)	
		else
			DrawSpriteScreenXY(57,  temp0, 54)
		endif	


end function


event ObjectMain





	CheckTouchRect(0, 96, screen.xcenter, screen.ysize)
	if checkResult > -1
		arrayPos0 = checkResult
		temp0 = touchscreen[arrayPos0].xpos
		temp0 -= 56
		temp1 = touchscreen[arrayPos0].ypos
		temp1 -= 184
		ATan2(temp2, temp0, temp1)
		temp2 += 16
		temp2 &= 255
		temp2 >>= 5
		switch temp2
		case 0
			inputDown.right = true
			break
		case 1
			inputDown.right = true
			inputDown.down = true
			break
		case 2
			inputDown.down = true
			break
		case 3
			inputDown.down = true
			inputDown.left = true
			break
		case 4
			inputDown.left = true
			break
		case 5
			inputDown.left = true
			inputDown.up = true
			break
		case 6
			inputDown.up = true
			break
		case 7
			inputDown.up = true
			inputDown.right = true
			break
		end switch
	end if
	temp2 = screen.xsize
	CheckTouchRect(screen.xcenter, 96, temp2, 240)
	if checkResult > -1
		inputDown.buttonA = 1
	end if
	if touchJump == 0
		inputPress.buttonA |= inputDown.buttonA
	end if
	touchJump = inputDown.buttonA

	temp0=screen.xsize
	temp0-=90
	CheckTouchRect(temp0, 0, screen.xsize, 40)
	if checkResult > -1
		inputpress[1].Start = true
	end if
	temp0 -= 37
	temp1 = screen.xsize
	temp1 -= 95
	CheckTouchRect(temp0, 50, temp1, 90)				
	if checkResult > -1
		inputDown.buttonB = 1
	end if
	if touchMisc == 0
		inputPress.buttonB |= inputDown.buttonB
	end if
	touchMisc = inputDown.buttonB

	ProcessObjectControl()



	switch object.state
	case MENU_STARTUP	//setup
		object.timer = 255
		Logo.Xpos = screen.xcenter
		object.state++
	//	PlayMusic(0)	
	break
	case MENU_LOWFADEIN	//fade in
		if object.timer > -1
			object.timer -= 4
		else
			object.timer = 0	
		end if
		SetClassicFade(0, 0, 0, Object.Timer)
		if object.timer == 0
			PlayMusic(0)	
			music.volume = 75
			object.state++
		end if
	break
	case MENU_LOGOMOVEIN	//logo move in
		object.timer++
		object.alpha += 8
		if object.timer == 120
			object.timer = 0
			Object.MenuChoose = 0
			object.state++
		end if	
	break
	case MENU_MAINCARD
	//Controls
		if inputpress.down == 1
			if Object.MenuChoose > 1
				Object.MenuChoose = 0
			else	
				Object.MenuChoose++
			end if
			PlaySfx(SfxName[Score Add], 0)			
		end if

		if inputpress.up == 1
			if Object.MenuChoose < 1
				Object.MenuChoose = 2
			else	
				Object.MenuChoose--
			end if
			PlaySfx(SfxName[Score Add], 0)			
		end if


		if inputpress.buttonA == 1
			inputPress.start = 1
		end if


		if inputPress.start == 1
			PlaySfx(SfxName[Lamp Post], 0)
			switch Object.MenuChoose
			case 0	//lol
				object.timer = 0
				object.state = MENU_GOTOCHARSEL
			break		
			case 1
				object.timer = 0
				object.state = 10			
			break
			case 2	//lol
				object.state = 99
			break
			end switch
		end if	

		if inputpress.buttonB == 1
			object.state = 99
		end if
	break
	case MENU_GOTOCHARSEL	//transition to next thing
		object.alpha -= 8
		if object.alpha <= 0
			object.alpha = 0
			Object.PlayerSelection = 0
			object.state = MENU_FADEINTOCHARSEL
		end if
	break
	case MENU_CHARASEL


		if inputpress.buttonA == 1
			inputPress.start = 1
		end if

		if object.alpha <= 255
			object.alpha += 8			
		end if

	
		if inputpress.right == 1
			if Object.PlayerSelection > 2
				Object.PlayerSelection = 0
			else	
				Object.PlayerSelection++
			end if
			PlaySfx(SfxName[Score Add], 0)			
		end if

		if inputpress.left == 1
			if Object.PlayerSelection < 1
				Object.PlayerSelection = 3
			else	
				Object.PlayerSelection--
			end if
			PlaySfx(SfxName[Score Add], 0)			
		end if
	

		if inputpress.start == 1
			StopMusic()
			PlaySfx(SfxName[Thunder], 0)
			PlaySfx(SfxName[Lamp Post], 0)
			options.gamemode = 0
			object.timer = 0
			object.state = MENU_STARTGAME
		end if

		if inputPress.buttonB == 1
			PlaySfx(SfxName[Skidding], 0)
			object.state = 6	//exiting what fun
		end if			
	break
	case MENU_LOWFADEOUTCHARASEL
		object.alpha -= 16
		if object.alpha <= 0
			object.alpha = 0
			object.state = 8
		end if
	break
	case MENU_FADEINTOCHARSEL
		if object.alpha < 255
			object.alpha += 8		
		end if
		object.timer++
		temp0 = screen.xcenter
		temp0 -= 127
		if object.timer >= temp0
			object.timer = 0
			object.state = MENU_CHARASEL	
		end if	
	break
	case MENU_FADEINTOMAINCARD
		object.alpha += 8
		if object.alpha >= 255
			object.state = MENU_MAINCARD
		end if
	break

	case MENU_STARTGAME
		object.timer += 4
		object.alpha -= 8

		options.gamemode = 0
		object.scale += 64
		if object.timer >= 400
			switch Object.PlayerSelection 
			case 0
				stage.playerlistpos = 1
			break
			case 1
				stage.playerlistpos = 0
			break
			case 2
				stage.playerlistpos = 2
			break
			end switch
			stage.activeList = BONUS_STAGE
			stage.listPos = 2
			LoadStage()
		end if

	break

	case 10

		music.volume -= 32
		object.timer += 8
		SetClassicFade(0, 0, 0, Object.Timer)
		if object.timer >= 255
			music.volume = 0
			StopMusic()
			options.gamemode = 1			
			stage.playerlistpos = 0
			stage.activeList = BONUS_STAGE
			stage.listPos = 2
			LoadStage()				
		end if
	break


	case 99	//exit
		music.volume -= 32
		object.timer += 8
		SetClassicFade(0, 0, 0, Object.Timer)
		if object.timer >= 255
			music.volume = 0
			StopMusic()
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 6
			LoadStage()				
		end if
	break
	end switch

	//Techinal Stuff

	TileLayer[0].Angle = 128


	// X/Z movement
	Sin(TileLayer[0].XPos, TileLayer[0].Angle)
	Cos(TileLayer[0].ZPos, TileLayer[0].Angle)
	TileLayer[0].XPos *= 0x12800
	TileLayer[0].ZPos *= 0x12800
	TileLayer[0].XPos += object.xpos
	TileLayer[0].ZPos += object.zpos


	// Just take Sonic's Y Position, smooth it out a bit, and offset it by 88 pixels
	TileLayer[0].YPos = object.YPos
	TileLayer[0].YPos /= 3
	TileLayer[0].YPos += 0x1280000	

end event


event ObjectDraw
	//Not Dead
	DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 128)

	//SLIDING STUFF
	temp0 = object.extraoffset	
	temp0 -= object.extratarget
	Abs(temp0)
	if temp0 < ST.xspeed // if object.extraoffset is less then ST.xspeed/15
		object.extraoffset = object.extratarget 	// offset = target
	else	// if its not
		if object.extraoffset < object.extratarget // if offset is less then target
			object.extraoffset += ST.xspeed	//go left
		end if
		if object.extraoffset > object.extratarget // if offset is greater then target
			object.extraoffset -= ST.xspeed	//go right
		end if
	end if
	object.InkEffect = INK_ALPHA
	switch object.state
		case MENU_LOGOMOVEIN
			object.extratarget = screen.xsize
			object.extratarget += 20

			temp3 = screen.xsize
			temp3 += screen.xcenter
			temp3 -= object.extraoffset
			if screen.xsize < 426
				temp3 /= 2
			end if


			//LOGO
			DrawSpriteScreenFX(0, FX_INK, temp3, screen.ycenter)	
			//BUTTONS
			DrawSpriteScreenFX(1, FX_INK, temp3, screen.ycenter)	
			temp3 -= 16
			DrawSpriteScreenFX(6, FX_INK, temp3, screen.ycenter)
		break
		case MENU_MAINCARD
		case MENU_FADEINTOMAINCARD
		case 99	//exit

			temp3 = screen.xsize
			temp3 -= screen.xcenter
			temp3 -= 20

			if screen.xsize < 426
				temp3 /= 2
			end if

			//dont move
			object.extratarget = 0
			//LOGO
			DrawSpriteScreenFX(0, FX_INK, temp3, screen.ycenter)	
			//BUTTONS
			DrawSpriteScreenFX(1, FX_INK, temp3, screen.ycenter)	


				switch Object.MenuChoose
					case STARTGAME
						DrawSpriteScreenFX(2, FX_INK, temp3, screen.ycenter)	
						temp4 = screen.ycenter
						temp3 -= 20
						DrawSpriteScreenFX(5, FX_INK, temp3, temp4)		
					break
					case ENDLESSMODE
						DrawSpriteScreenFX(3, FX_INK, temp3, screen.ycenter)	
						temp4 = screen.ycenter
						temp4 += 23
						temp3 -= 20
						DrawSpriteScreenFX(5, FX_INK, temp3, temp4)		
					break					
					case RETURNTOMENU
						DrawSpriteScreenFX(4, FX_INK, temp3, screen.ycenter)	
						temp4 = screen.ycenter
						temp4 += 46
						temp3 -= 20
						DrawSpriteScreenFX(5, FX_INK, temp3, temp4)							
					break						
				end switch
			temp3 += 4	
			

			DrawSpriteScreenFX(6, FX_INK, temp3, screen.ycenter)		

			if saveRAM[6900] == 1

			temp3 += 380
			temp4 = screen.ycenter
			temp4 += 90
			DrawSpriteScreenFX(18, FX_INK, temp3, temp4)		

			end if
			break
		case MENU_GOTOCHARSEL

			temp3 = screen.xsize
			temp3 -= screen.xcenter
			temp3 -= 20
			if screen.xsize < 426
				temp3 /= 2
			end if			

			object.extratarget = 0
			//LOGO
			DrawSpriteScreenFX(0, FX_INK, temp3, screen.ycenter)	
			//BUTTONS
			DrawSpriteScreenFX(1, FX_INK, temp3, screen.ycenter)	
			//render

			temp3 -= 16	
			DrawSpriteScreenFX(6, FX_INK, temp3, screen.ycenter)	
		break		
		case MENU_FADEINTOCHARSEL
			DrawSpriteScreenFX(7, FX_INK, screen.xcenter, screen.ycenter)	

			object.extratarget = screen.xsize
			object.extratarget += 20

			temp3 = screen.xsize
			temp3 += screen.xcenter
			temp3 -= object.extraoffset

			DrawSpriteScreenFX(9, FX_INK, temp3, screen.ycenter)	
		break
		case MENU_CHARASEL
		case MENU_LOWFADEOUTCHARASEL
			object.extratarget = 0
		
			DrawSpriteScreenFX(7, FX_INK, screen.xcenter, screen.ycenter)	

			temp3 = screen.xsize
			temp3 -= screen.xcenter
		
			switch Object.PlayerSelection
			case 0	//Tails!
				DrawSpriteScreenFX(14, FX_INK, temp3, screen.ycenter)	
				DrawSpriteScreenFX(9, FX_INK, temp3, screen.ycenter)				
			break
			case 1	//Sonic!
				DrawSpriteScreenFX(15, FX_INK, temp3, screen.ycenter)	
				DrawSpriteScreenFX(8, FX_INK, temp3, screen.ycenter)
			break			
			case 2	//Knuckles!
				DrawSpriteScreenFX(16, FX_INK, temp3, screen.ycenter)	
				DrawSpriteScreenFX(10, FX_INK, temp3, screen.ycenter)	
			break			
			case 3	//Amy!
				DrawSpriteScreenFX(17, FX_INK, temp3, screen.ycenter)	
				DrawSpriteScreenFX(11, FX_INK, temp3, screen.ycenter)	
			break			
			end switch	
		

			DrawSpriteScreenFX(13, FX_INK, temp3, screen.ycenter)	
			temp3 += 200
			DrawSpriteScreenFX(12, FX_INK, temp3, screen.ycenter)	
		break	
		case MENU_STARTGAME
			object.extratarget = 0
		
			DrawSpriteScreenFX(7, FX_INK, screen.xcenter, screen.ycenter)	

			temp3 = screen.xcenter	

			DrawSpriteScreenFX(13, FX_INK, temp3, screen.ycenter)	
			temp3 += 200
			DrawSpriteScreenFX(12, FX_INK, temp3, screen.ycenter)	

			temp3 -= 200

			switch Object.PlayerSelection
			case 0	//Sonic!
				DrawSpriteScreenFX(9, FX_SCALE, temp3, screen.ycenter)	
			break
			case 1	//Tails!
				DrawSpriteScreenFX(8, FX_SCALE, temp3, screen.ycenter)	
			break			
			case 2	//Knuckles!
				DrawSpriteScreenFX(10, FX_SCALE, temp3, screen.ycenter)	
			break			
			case 3	//Amy!
				DrawSpriteScreenFX(11, FX_SCALE, temp3, screen.ycenter)	
			break			
			end switch	
		

		break		
		default
			object.extratarget = 0
			break
	end switch

	if saved.mobile == 1
		CallFunction(TouchControls)
	end if


end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects2.gif")
	
	//logo
	SpriteFrame(32, -96, 184, 57, 327, 1)	//0
	//buttons
	SpriteFrame(128, 0, 67, 57, 376, 59)		//1

	//HIGHLIGHTED
	//Start
	SpriteFrame(156, 0, 39, 11, 472, 59)		//2
	//Endless
	SpriteFrame(140, 23, 55, 11, 456, 82)		//3
	//Start
	SpriteFrame(130, 46, 65, 11, 446, 105)		//4

	//page icon
	SpriteFrame(130, 0, 15, 11, 360, 59)		//5

	//render
	SpriteFrame(-150, -64, 178, 147, 1, 1)		//6

	//choose
	SpriteFrame(-80, -102, 166, 11, 345, 117)		//7

	//char renders
	//Sonic Render
	SpriteFrame(-56, -42, 103, 148, 1, 229)		//8
	//Tails Render
	SpriteFrame(-84, -32, 160, 116, 351, 246)		//9
	//Knux Render
	SpriteFrame(-64, -42, 129, 134, 0, 378)		//10
	//Amy Render
	SpriteFrame(-64, -44, 130, 149, 381, 363)		//11

	//Arrow Left
	SpriteFrame(-120, 0, 32, 32, 396, 153)		//12
	//Arrow Right
	SpriteFrame(-120, 0, 32, 32, 429, 153)		//13


	//Easy
	SpriteFrame(-16, -72, 31, 11, 471, 155)		//14
	//Normal
	SpriteFrame(-22, -72, 47, 11, 463, 171)		//15
	//Hard
	SpriteFrame(-16, -72, 31, 11, 471, 187)		//16
	//Extreme
	SpriteFrame(-26, -72, 57, 11, 454, 201)		//17	
	
	// 18 - battery bar
	SpriteFrame(-194, -22, 18, 32, 344, 153)
	SpriteFrame(-194, -12, 24, 8, 322, 126)//19
	SpriteFrame(-194, -2, 24, 8, 322, 136)	//20
	SpriteFrame(-194,  8, 24, 8, 322, 146)	//21
	SpriteFrame(-194,  18, 24, 8, 322, 156)	//22



	// 23 - SLENDERMAN!!
	SpriteFrame(-30, -24, 63, 112, 339, 1)


	SpriteFrame(-34, -30, 68, 30, 0, 412) //107 Static
	SpriteFrame(-34, -30, 68, 30, 0, 442)//25
	SpriteFrame(-34, -30, 68, 30, 0, 472)//26
	SpriteFrame(-34, -30, 68, 30, 68, 412)//27
	SpriteFrame(-34, -30, 68, 30, 68, 442)//28
	SpriteFrame(-34, -30, 68, 30, 68, 472)//29
	SpriteFrame(-34, -30, 68, 30, 136, 412)//30
	SpriteFrame(-34, -30, 68, 30, 136, 442)//31
	SpriteFrame(-34, -30, 68, 30, 136, 472)//32


	SpriteFrame(-24, -30, 64, 64, 0, 413) //33


	// lazy filler
	SpriteFrame(-48, -24, 20, 24, 52, 59) //34 ring
	SpriteFrame(-48, -23, 20, 22, 83, 180) //35 ring
	SpriteFrame(-82, -82, 176, 166, 1, 513)	//36	Crafting UI
	SpriteFrame(0, 0, 20, 18, 178, 580)	//37	buton
	SpriteFrame(0, 0, 0, 0, 0, 0)	//38
	SpriteFrame(-240, -82, 147, 166, 199, 513)	//39	Reciepe UI
	SpriteFrame(0, 0, 18, 7, 178, 615)	//40 12x
	SpriteFrame(0, 0, 18, 7, 178, 623)	//41 7x
	SpriteFrame(-53, -66, 116, 54, 347, 513)	//42 the thing sob
	SpriteFrame(0, 0, 18, 18, 464, 513)	//43 paper chainsaw
	SpriteFrame(-53, -66, 116, 54, 347, 568)	//44 the thing sob
	SpriteFrame(0, 0, 16, 16, 8, 680)	//45 chainsaw slotted
	SpriteFrame(0, 0, 12, 21, 245, 1)	//46
	SpriteFrame(0, 0, 12, 21, 245, 1)	//47
	SpriteFrame(0, 0, 12, 21, 245, 1)	//48	

	SpriteFrame(0,0,64,66,126,257) //49 DPAD
	SpriteFrame(26,0,12,25,242,321) //50 DPAD up press
	SpriteFrame(26,0,12,26,242,359) //51 DPAD down press
	SpriteFrame(0,25,26,13,215,358) //52 DPAD left press
	SpriteFrame(0,25,26,13,215,372) //53 DPAD right press
	SpriteFrame(0,0,48,48,191,257) //54 A button
	SpriteFrame(0,0,48,48,191,306) //55 A button down

	SpriteFrame(32,0,16,16,174,325) // 56 Pause Button

	SpriteFrame(-8,-8,48,48,240,223) // 57 Super Button - Mobile

	SpriteFrame(-8,-8,48,48,240,272) // 58 Super Button - Mobile

	SpriteFrame(0,0,16,16,174,325) // 59 Pause Button again

	SpriteFrame(0,0,34,34,165,360) // 60 Pause Button

	SetMusicTrack("SlenderMenu.ogg", 0, 1)

	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[4].Type = TypeName[Main Menu]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[4].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[4].DrawOrder = 6
	
	// Cycle through all UFO objects in the scene and set them up
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Main Menu]
			
			// Due to the difference between editor and game, turn YPos into ZPos here
			Object[4].ZPos = Object[ArrayPos0].YPos

			ResetObjectEntity(ArrayPos0, TypeName[Blank Object], 0, 0, 0)
		
		end if
		
		ArrayPos0++
	loop

end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(0, 0, 24, 16, 206, 58)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
